package com.ruoyi.baselibrary.infobasepolicy.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.baselibrary.infobasecategory.service.IInfoBaseCategoryService;
import com.ruoyi.baselibrary.infobasepolicy.domain.InfoBasePolicy;
import com.ruoyi.baselibrary.infobasepolicy.service.IInfoBasePolicyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 政策法规Controller
 *
 * @author ruoyi
 * @date 2022-01-12
 */
@Api(tags = "基础库-政策库管理")
@RestController
@RequestMapping("/infoBasePolicy/infoBasePolicy")
public class InfoBasePolicyController extends BaseController {
    @Autowired
    private IInfoBasePolicyService infoBasePolicyService;


    @Autowired
    private IInfoBaseCategoryService baseCategoryService;

    /**
     * 查询政策法规列表
     */
    @ApiOperation("查询基础库-政策列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String"),
    })
    @PreAuthorize("@ss.hasPermi('infoBasePolicy:infoBasePolicy:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoBasePolicy infoBasePolicy) {
        startPage();
        String baseCategoryByParentId = baseCategoryService.getBaseCategoryByParentId(infoBasePolicy.getCategory());
        infoBasePolicy.setCategoryIds(baseCategoryByParentId);
        infoBasePolicy.setCategory(null);
        List<InfoBasePolicy> list = infoBasePolicyService.selectInfoBasePolicyList(infoBasePolicy);
        for (InfoBasePolicy basePolicy : list) {
            basePolicy.setCategoryName(baseCategoryService.getBaseCategoryName(basePolicy.getCategory()));
        }
        return getDataTable(list);
    }

    /**
     * 导出政策法规列表
     */
    @PreAuthorize("@ss.hasPermi('infoBasePolicy:infoBasePolicy:export')")
    @Log(title = "政策法规", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoBasePolicy infoBasePolicy) {
        List<InfoBasePolicy> list = infoBasePolicyService.selectInfoBasePolicyList(infoBasePolicy);
        ExcelUtil<InfoBasePolicy> util = new ExcelUtil<InfoBasePolicy>(InfoBasePolicy.class);
        util.exportExcel(response, list, "政策法规数据");
    }

    /**
     * 获取政策法规详细信息
     */
    @ApiOperation("获取政策法规详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "政策库Id", dataType = "Long"),
    })
    @PreAuthorize("@ss.hasPermi('infoBasePolicy:infoBasePolicy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(infoBasePolicyService.selectInfoBasePolicyById(id));
    }

    /**
     * 新增政策法规
     */
    @ApiOperation("政策库增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "类别", dataType = "Long"),
            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String"),
            @ApiImplicitParam(name = "fileUrl", value = "文件下载地址", dataType = "String"),
            @ApiImplicitParam(name = "fileSize", value = "文件大小", dataType = "String")
    })
    @PreAuthorize("@ss.hasPermi('infoBasePolicy:infoBasePolicy:add')")
    @Log(title = "政策法规", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoBasePolicy infoBasePolicy) {
        return toAjax(infoBasePolicyService.insertInfoBasePolicy(infoBasePolicy));
    }

    /**
     * 修改政策法规
     */
    @PreAuthorize("@ss.hasPermi('infoBasePolicy:infoBasePolicy:edit')")
    @Log(title = "政策法规", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoBasePolicy infoBasePolicy) {
        return toAjax(infoBasePolicyService.updateInfoBasePolicy(infoBasePolicy));
    }

    /**
     * 删除政策法规
     */
    @ApiOperation("政策库删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id数组", dataType = "Long"),
    })
    @PreAuthorize("@ss.hasPermi('infoBasePolicy:infoBasePolicy:remove')")
    @Log(title = "政策法规", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(infoBasePolicyService.deleteInfoBasePolicyByIds(ids));
    }
}
